package com.anmoyi.web.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.anmoyi.common.AppError;
import com.anmoyi.common.Packet;
import com.anmoyi.common.StringUtil;
import com.anmoyi.common.TokenUtil;
import com.anmoyi.common.exception.TokenException;
import com.anmoyi.model.po.Custommassage;
import com.anmoyi.model.po.User;
import com.anmoyi.model.po.Usertemper;
import com.anmoyi.service.UserService;
import com.anmoyi.service.UsertemperService;
import com.anmoyi.web.ao.CustommassageAO;
import com.anmoyi.web.ao.UsertemperAO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
public class UsertemperController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(UsertemperController.class);
    @Autowired
    private UsertemperService usertemperService;
    @Autowired
    private UserService userService;

    @PostMapping(value = "/addUsertemper")
    public String addUsertemper(@RequestBody String requestString) {
        logger.info("温度计日志: ");
        logger.info(requestString);
        Packet packet = null;
        try {
            packet = JSON.parseObject(requestString, Packet.class);
        } catch (Exception e) {
            logger.error("添加温度计数据解析异常1\n" + e);
            return responseToClient(AppError.APP_JSON_INVALID_ERROR);
        }
        UsertemperAO usertemperAO = null;
        try {
            //packet.getData() 为jsonobject
            usertemperAO = JSON.parseObject(packet.getData().toString(), UsertemperAO.class);
        } catch (Exception e) {
            logger.error("添加温度计数据创建AO异常2", e);
            return responseToClient(AppError.APP_ARGS_ERROR);
        }
        if (StringUtil.isNullOrBlank(usertemperAO)) {
            logger.error("添加温度计数据无数据异常3");
            return responseToClient(AppError.APP_ARGS_ERROR);
        }
        Usertemper usertemper = new Usertemper();
        usertemper.setUserId(usertemperAO.getUserId());
        usertemper.setDeviceid(usertemperAO.getDeviceid());
        usertemper.setTemperature(usertemperAO.getTemperature());
        usertemperService.addUsertemper(usertemper);
        logger.info("添加温度计数据6");
        Usertemper list = usertemperService.selectlastByuserId(usertemperAO.getUserId());
        int count=usertemperService.selectbind(usertemper.getDeviceid(), usertemper.getUserId()) ;
        if (count == 0) {
            usertemperService.devicebinduser(usertemper.getDeviceid(), usertemper.getUserId());
        }
        return responseToClientWithData(AppError.APP_OK, list);
    }

    @PostMapping(value = "/deleteUsertemper")
    public String deleteUsertemper(@RequestBody String requestString) {
        logger.info("删除温度计数据1: ");
        logger.info(requestString);

        Packet packet = null;
        try {
            packet = JSON.parseObject(requestString, Packet.class);
        } catch (Exception e) {
            logger.error("删除温度计数据解析异常1\n" + e);
            return responseToClient(AppError.APP_JSON_INVALID_ERROR);
        }
        UsertemperAO usertemperAO = new UsertemperAO();
        try {
            //packet.getData() 为jsonobject
            System.out.println(requestString);
            JSONObject data = JSONObject.parseObject(requestString);
            JSONObject jo = data.getJSONObject("data");
            usertemperAO.setId(data.getInteger("id"));
        } catch (Exception e) {
            logger.error("删除温度计数据创建AO异常2", e);
            return responseToClient(AppError.APP_ARGS_ERROR);
        }
        if (StringUtil.isNullOrBlank(usertemperAO)) {
            logger.error("删除温度计数据无数据异常3");
            return responseToClient(AppError.APP_ARGS_ERROR);
        }
        usertemperService.deleteByPrimaryKey(usertemperAO.getId());
        logger.info("删除温度计数据无数据异常4");
        return responseToClient(AppError.APP_OK);
    }

    @PostMapping(value = "/queryUsertemper")
    public String queryUsertemper(@RequestBody String requestString) {
        logger.info("个人温度数据查询是: ");
        logger.info(requestString);
        Packet packet = null;
        try {
            packet = JSON.parseObject(requestString, Packet.class);
        } catch (Exception e) {
            logger.error("添加温度计数据解析异常1\n" + e);
            return responseToClient(AppError.APP_JSON_INVALID_ERROR);
        }
        UsertemperAO usertemperAO = null;
        try {
            //packet.getData() 为jsonobject
            usertemperAO = JSON.parseObject(packet.getData().toString(), UsertemperAO.class);


        } catch (Exception e) {
            logger.error("个人温度数据查询异常2", e);
            return responseToClient(AppError.APP_ARGS_ERROR);
        }

        String phone = null;
        try {
            phone = TokenUtil.getEmail(packet.getToken());
        } catch (TokenException e) {
            logger.error("个人温度数据查询异常3", e);
            return responseToClient(AppError.APP_TOKEN_INVALID_ERROR);
        }
        User user = userService.getByPhone(phone);
        if (null == user) {
            logger.error("个人温度数据查询异常4");
            return responseToClient(AppError.APP_TOKEN_INVALID_ERROR);
        }

        this.token = user.getToken();
        List<Usertemper> list = usertemperService.selectByuserId(usertemperAO.getUserId(), usertemperAO.getCtime());
        logger.info("个人温度数据查询5");
        return responseToClientWithData(AppError.APP_OK, list);
    }

    @PostMapping(value = "/bindtemper")
    public String bindtemper(@RequestBody String requestString) {
        logger.info("绑定温度计参数是: ");
        logger.info(requestString);
        Packet packet = null;
        try {
            packet = JSON.parseObject(requestString, Packet.class);
        } catch (Exception e) {
            logger.error("绑定温度计解析异常1\n" + e);
            return responseToClient(AppError.APP_JSON_INVALID_ERROR);
        }
        UsertemperAO usertemperAO = null;
        try {
            //packet.getData() 为jsonobject
            usertemperAO = JSON.parseObject(packet.getData().toString(), UsertemperAO.class);
        } catch (Exception e) {
            logger.error("绑定温度计异常2", e);
            return responseToClient(AppError.APP_ARGS_ERROR);
        }
        String phone = null;
        try {
            phone = TokenUtil.getEmail(packet.getToken());
        } catch (TokenException e) {
            logger.error("绑定温度计异常3", e);
            return responseToClient(AppError.APP_TOKEN_INVALID_ERROR);
        }
        User user = userService.getByPhone(phone);
        if (null == user) {
            logger.error("绑定温度计异常4");
            return responseToClient(AppError.APP_TOKEN_INVALID_ERROR);
        }
        this.token = user.getToken();
        usertemperService.bindtemper(usertemperAO.getUserId(), usertemperAO.getDeviceid());
        logger.info("绑定温度计5");
        return responseToClient(AppError.APP_OK);
    }
    @PostMapping(value = "/listbindtemper")
    public String listbindtemper(@RequestBody String requestString) {
        logger.info("绑定温度计列表参数是: ");
        logger.info(requestString);
        Packet packet = null;
        try {
            packet = JSON.parseObject(requestString, Packet.class);
        } catch (Exception e) {
            logger.error("绑定温度计列表解析异常1\n" + e);
            return responseToClient(AppError.APP_JSON_INVALID_ERROR);
        }
        UsertemperAO usertemperAO = null;
        try {
            //packet.getData() 为jsonobject
            usertemperAO = JSON.parseObject(packet.getData().toString(), UsertemperAO.class);
        } catch (Exception e) {
            logger.error("绑定温度计异常2", e);
            return responseToClient(AppError.APP_ARGS_ERROR);
        }
        String phone = null;
        try {
            phone = TokenUtil.getEmail(packet.getToken());
        } catch (TokenException e) {
            logger.error("绑定温度计异常3", e);
            return responseToClient(AppError.APP_TOKEN_INVALID_ERROR);
        }
        User user = userService.getByPhone(phone);
        if (null == user) {
            logger.error("绑定温度计异常4");
            return responseToClient(AppError.APP_TOKEN_INVALID_ERROR);
        }
        this.token = user.getToken();
        List list=usertemperService.listbindtemper(usertemperAO.getUserId());
        logger.info("绑定温度计5");
        return responseToClientWithData(AppError.APP_OK, list);
    }
    @PostMapping(value = "/listdevicebinduser")
    public String listdevicebinduser(@RequestBody String requestString) {
        logger.info("绑定温度计列表参数是: ");
        logger.info(requestString);
        Packet packet = null;
        try {
            packet = JSON.parseObject(requestString, Packet.class);
        } catch (Exception e) {
            logger.error("绑定温度计列表解析异常1\n" + e);
            return responseToClient(AppError.APP_JSON_INVALID_ERROR);
        }
        UsertemperAO usertemperAO = null;
        try {
            //packet.getData() 为jsonobject
            usertemperAO = JSON.parseObject(packet.getData().toString(), UsertemperAO.class);
        } catch (Exception e) {
            logger.error("绑定温度计异常2", e);
            return responseToClient(AppError.APP_ARGS_ERROR);
        }
        String phone = null;
        try {
            phone = TokenUtil.getEmail(packet.getToken());
        } catch (TokenException e) {
            logger.error("绑定温度计异常3", e);
            return responseToClient(AppError.APP_TOKEN_INVALID_ERROR);
        }
        User user = userService.getByPhone(phone);
        if (null == user) {
            logger.error("绑定温度计异常4");
            return responseToClient(AppError.APP_TOKEN_INVALID_ERROR);
        }
        this.token = user.getToken();
        List list=usertemperService.listdevicebinduser(usertemperAO.getDeviceid());
        logger.info("绑定温度计5");
        return responseToClientWithData(AppError.APP_OK, list);
    }
}
